const { ipcRenderer, contextBridge } = require('electron')

const sendUrlToMain = async (url) => {
  let result = await ipcRenderer.invoke('on-url-event', url)
  return result 
}

const alert = (msg) => {
  ipcRenderer.send('on-alert-event', msg)
}

const openDetailsWindow = async (url) => {
  ipcRenderer.send('on-openwin-event', url)
}

const initOpenDialogEvent = () => {
  ipcRenderer.send('on-opendialog-event')
}

const responseFromMain = () => {
  return new Promise((resolve) => {
    ipcRenderer.on('on-response-event', (e, msg) => {
      resolve()
    })
  })
}

const closeWindow = () => {
  ipcRenderer.send('on-close-window')
}

const loadGalleryImg = async () => {
  const result = await ipcRenderer.invoke('on-load-gallery-img')
  return result
}

contextBridge.exposeInMainWorld('exposeApi', {
  sendUrlToMain,
  alert,
  openDetailsWindow,
  initOpenDialogEvent,
  responseFromMain,
  closeWindow,
  loadGalleryImg
})